<?php

namespace app\site\controller;

use app\common\model\ShopGoodsModel;
use app\common\model\ShopSiteGroupModel;
use org\util\TreeUtil;
use think\Request;

/**
 * Class SiteGroupController
 *
 * @package \app\site\controller
 */
class ShopSiteGroupController extends BaseController
{
    private $siteGroupeModel;
    private $catList;

    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->siteGroupeModel = new ShopSiteGroupModel();
        $list = $this->siteGroupeModel->where('site_id', $this->getSiteId())->select();
        $this->catList = TreeUtil::tree($list->toArray(), 'cat_name', 'cid');
    }

    public function index()
    {
        $this->assign('catList', $this->catList);
        return $this->fetch();
    }

    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            if ($this->request->has('is_batch')) { //批量模式
                $data['cat_name'] = $this->request->param('cat_names');
            }
            $result = $this->validate($data, 'ShopSiteGroupValidate');
            if ($result !== true) {
                $this->error($result);
            }
            $data['site_id'] = $this->getSiteId();
            if ($this->request->has('is_batch')) {
                $catNames = explode("\r\n", $data['cat_name']);
                $dataList = [];
                foreach ($catNames as $v) {
                    unset($data['cat_names']);
                    unset($data['is_batch']);
                    $data['cat_name'] = $v;
                    $dataList[]       = $data;
                }
                $this->siteGroupeModel->saveAll($dataList);
            } else {
                $this->siteGroupeModel->addData($data);
            }
            $this->siteGroupeModel->updateCache($this->getSiteId());
            $this->success('添加成功', url('index'));
        }
        $this->assign('catList', $this->catList);
        return $this->fetch();
    }

    /**
     * 编辑分组
     * @return mixed
     */
    public function edit()
    {
        if ($this->request->isAjax()) {
            $data   = $this->request->param();
            $result = $this->validate($data, 'ShopSiteGroupValidate');
            if ($result !== true) {
                $this->error($result);
            }
            $this->siteGroupeModel->editData($data);
            $this->siteGroupeModel->updateCache($this->getSiteId());
            $this->success('操作成功！', url('index'));
        }
        $cid      = $this->request->param('cid/d', 0);
        $info     = $this->siteGroupeModel->get($cid);
        $catList = [];
        foreach ($this->catList as $k => $v) {
            //父栏目select状态
            $v['selected'] = $info['pid'] == $v['cid'] ? 'selected' : '';
            //子栏目disabled
            $disabled      = \org\util\TreeUtil::isChild($this->catList, $v['cid'], $cid, 'cid') || $cid == $v['cid'] ? 'disabled' : '';
            $v['disabled'] = $disabled;
            $catList[]    = $v;
        }
        $this->assign('catList', $catList);
        $this->assign('info', $info);
        return $this->fetch();
    }
    /**
     *栏目排序
     */
    public function updateOrder()
    {
        $listOrder = $this->request->param('list_order/a');
        foreach ($listOrder as $cid => $order) {
            $cid        = intval($cid);
            $order      = intval($order);
            $dataList[] = ["cid" => $cid, "cat_order" => $order];
        }
        $this->siteGroupeModel->saveAll($dataList);
        $this->siteGroupeModel->updateCache($this->getSiteId());
        $this->success('排序成功！');
    }
    /**
     * 更新站点缓存
     */
    public function updateCache()
    {
        $this->siteGroupeModel->updateCache($this->getSiteId());
        $this->success('操作成功！', url('index'));
    }
    /**
     * 删除分组
     */
    public function del()
    {
        $cid = $this->request->param('cid/d');
        $goodsModel=new ShopGoodsModel();
        if ($this->siteGroupeModel->where('pid', $cid)->find()) {
            $this->error('该分组还存在下级分组，不允许删除！');
        } else {
            if ($goodsModel->where('site_cid', $cid)->find()) {
                $this->error('该分组下还存在数据，不允许删除！');
            }
            $this->siteGroupeModel->delData($cid);
            $this->siteGroupeModel->updateCache($this->getSiteId());
            $this->success('删除成功！');
        }
    }
}
